package com.vanvalt.payhui.modules.pay.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.vanvalt.payhui.modules.pay.entity.Question;
import com.vanvalt.payhui.modules.pay.service.QuestionService;

/**
 * @author Xiaoyang
 * 常见问题 FAQ（Frequently Asked Question）
 */
@Controller
@RequestMapping(value = "${adminPath}/pay/faq")
public class QuestionController extends BaseController {

	@Autowired
	private QuestionService questionService;
	
	@ModelAttribute
	public Question get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return questionService.get(id);
		}else{
			return new Question();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Question question, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<Question> page = questionService.findPage(new Page<Question>(request, response), question); 
        model.addAttribute("page", page);
		return "modules/pay/faqList";
	}
	
	@RequestMapping(value = "form")
	public String form(Question question, Model model) {
		
		model.addAttribute("question", question);
		return "modules/pay/faqForm";
	}
	
	@RequestMapping(value = "save")
	public String save(Question question, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, question)){
			return form(question, model);
		}
		questionService.save(question);
		addMessage(redirectAttributes, "保存常见问题信息'" + StringUtils.abbr(question.getTitle(),50) + "'成功");
		return "redirect:" + adminPath + "/pay/faq/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Question question, @RequestParam(required=false) Boolean isRe, RedirectAttributes redirectAttributes) {
		
		questionService.delete(question);
		addMessage(redirectAttributes, (isRe!=null&&isRe?"发布":"删除")+"常见问题信息成功");
		return "redirect:" + adminPath + "/pay/faq/?repage";
	}
	
}
